package com.taikang.hmp;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import spApi.Bind;
import spApi.BindResp;
import spApi.Deliver;
import spApi.SGIP_Command;
import spApi.SGIP_Exception;
import spApi.Submit;
import spApi.SubmitResp;
import spApi.Unbind;
import spApi.UnbindResp;

public class Taikangtest {
	static String uid = "8e78d11c-de4f-4b3c-9122-b0274fbac1c8";
	public static void main(String[] args) {
//		regist();
	//	identify();
	//	passreset();
		System.out.println(stringMD5("123456"));
		getToken();
//		String sessionid = login();
		//passreset();
//		getBaseinfo(sessionid);
	//	saveBaseinfo(sessionid);
//		getSdf("201509171511012819096712");
	//	getHemoglobinList("201509171511012819096712");
		//saveSugarPlan(sessionid);
//		getSugarPlan(sessionid);
//		getMedicinePlan(sessionid);
		//getWarnList(sessionid);
		//setWarnState(sessionid);
		//setWarnState(sessionid);
	//	getWarnCount(sessionid);
//		getMedicine("123");
//		saveSport(sessionid);
//		send("18513952782","短消息发送成功1234567");
	}
	/**登录
     * 
      */
     public static void send(String mobile,String message)
     {
    	 Socket so=null;
         OutputStream out=null;
         InputStream input=null;
         Bind command=null;
         Bind com=null;
         SGIP_Command sgip=null;
         SGIP_Command tmp=null;
         int i=0;
         try {
             sgip=new SGIP_Command();
             command=new Bind(399000,//nodeID 3+CP_ID
                              1, //login type
                              "jkxw",//login name
                              "jkxw");//login password
             int err;
             byte [] byte_content = new byte[140];
             Deliver deliver=null;
             Submit submit = null;
             SubmitResp submitresp=null;
             Bind active=null;
             Unbind term=null;
             BindResp resp=null;
             UnbindResp Unresp=null;
             so=new Socket("211.94.67.202",8801);
             out=new DataOutputStream(so.getOutputStream());
             input = new DataInputStream(so.getInputStream());
             err=command.write(out);//发送bind
             if(err!=0)
             {
                System.out.println("err"+err);
            }
             tmp=sgip.read(input);//接收sgip消息
	          if(sgip.getCommandID()==SGIP_Command.ID_SGIP_BIND_RESP)
		        {
		          resp=(BindResp)tmp;//强制转换为bindresp
              resp.readbody();//对消息进行解包
              System.out.println(resp.GetResult());
              }
             
    	 submit = new Submit(399000,//nod	e id同上
                 "106550102045",//cp_phone
                 "000000000000000000000",//付费号码
                 1,//接收短消息的手机数
                 "86"+mobile,//手机号码前面加86
                 "80710",//cp_id
                 "HYDX",//业务代码
                 0,//计费类型
                 "0",//短消息收费值
                 "0",//赠送话费
                 1,//代收标志
                 1,//引起MT的原因
                 9,//优先级
                 "",//短消息终止时间
                 "",//011125120000032+短消息定时发送时间
                 1,//状态报告标志
                 1,//GSM协议类型
                 1,//GSM协议类型
                 15,//短消息编码格式
                 0,//信息类型
                 12,//短消息长度
                 message);//短消息内容
   
    	 submit.write(out);//发送submit
         tmp=sgip.read(input);
         if(tmp.getCommandID()==SGIP_Command.ID_SGIP_SUBMIT_RESP)
         {
             submitresp=(SubmitResp)tmp;//强制转换
             submitresp.readbody();//解包
             System.out.println(submitresp.getResult());
         }

         //com.write(out);
         term=new Unbind(399000);
         term.write(out);//发送unbind
         tmp=sgip.read(input);
         if(sgip.getCommandID()==SGIP_Command.ID_SGIP_UNBIND_RESP)
         {
         Unresp=(UnbindResp)tmp;
         
         }
         
        out.close();
       so.close();
    }catch (SGIP_Exception e){
       System.out.println(e.toString());
       }
    catch (Exception e) {
            System.out.println(e.toString());
    } finally {
              try {
            	  out.close();
                  so.close();
                  //it just for debug
              } catch (Exception s) {
                      System.out.println(s.toString());
              }
    }
				
     }
     // 辅助函数: 使用 POST 方式发起一个 HTTP 请求, 并返回结果
     private static String httpGetUrl2(String url,String jsonStr)
     {
     	
     	 NameValuePair value0 =  new NameValuePair("jsonStr", jsonStr);
          NameValuePair value1 =  new NameValuePair("checkStr", stringMD5(jsonStr));

          NameValuePair[] orderInfo = {value0,value1};
          
         String response=new String();
         HttpClient client = new HttpClient();
        
         //使用POST方法
         PostMethod method = new UTF8PostMethod(url);
         
         method.setRequestBody(orderInfo);
      System.out.println(method.getRequestCharSet());
      System.out.println(method.getResponseCharSet());
         
         
         try{
             client.executeMethod(method);
             
             if(method.getStatusCode()==200)
             {
                 response = method.getResponseBodyAsString();
             }
         }catch (Exception ex)
         {
            ex.printStackTrace();
         }

         
        
         //释放连接
         method.releaseConnection();
         // 转换编码
         //return Utility.utf8Togb2312(response);
         return response;
     }
	/**注册
     * 
      */
     public static String regist()
     {
     	String jsonStr = "";
     	Map sportmap = new HashMap();
         sportmap.put("method", "regist");
         sportmap.put("version", "1.0");
         sportmap.put("token", "test");
         sportmap.put("client", "andro");
//         sportmap.put("username", "13910764604");
//         sportmap.put("password", stringMD5("123456"));
//         sportmap.put("name", "zhaocs");
//         sportmap.put("idno", "210905197807210546");
//         sportmap.put("mobile", "13910764604");
         
         sportmap.put("username", "13910764607");
         sportmap.put("password", stringMD5("123456"));
         sportmap.put("name", "zhaocs");
         sportmap.put("idno", "130223198405306843");
         sportmap.put("mobile", "13910764607");
         
         

        
   //     String url = "http://123.57.152.60:80/kwhealth/server/evaluationInterface.do";
//     String url = "http://hdhealthtest.taikang.com:80/hdhealth/app/doctor/diabetes/login.do";
         String url = "http://192.168.2.34:8080/hdhealth/app/doctor/diabetes/login.do";
     jsonStr = JSONObject.fromObject(getOutMap(sportmap)).toString();
     System.out.println(jsonStr);
         String result =  httpGetUrl(url,jsonStr);
         System.out.println(result);
         result = getInString(result);
         System.out.println(result);
         JSONObject obj = JSONObject.fromObject(result);
         
         return obj.getString("sessionid");
     }
     /**注册
      * 
       */
      public static String getToken()
      {
      	String jsonStr = "";
     

         
    //     String url = "http://123.57.152.60:80/kwhealth/server/evaluationInterface.do";
//      String url = "http://hdhealthtest.taikang.com:80/hdhealth/app/doctor/diabetes/login.do";
          String url = "https://www.touchsoft.com.cn/healthy/api/oauth/token?grant_type=authorization_code&third_id=HEALTH_HD&code=fa3a8bf3-7cb6-41c0-b23e-5c3af3b3ef94";

      System.out.println(url);
          String result =  httpGet(url);
          System.out.println(result);
          
          
          return result;
      }
       
       /**登录
        * 
         */
        public static String login()
        {
        	String jsonStr = "";
        	Map sportmap = new HashMap();
            sportmap.put("method", "login");
            sportmap.put("version", "1.0");
            sportmap.put("devicecode", "867711022435790");
            sportmap.put("username", "15652413511");
            sportmap.put("password", stringMD5("123456"));
//            sportmap.put("password", "E89ADC6050BA40ABBE43E942F79F116E");
//            sportmap.put("mobile", "zhaocs");
//            sportmap.put("password","03E20781034EB27C07A450DD4A669887" );
            //sportmap.put("password", stringMD5("zoubin02"));
            sportmap.put("token", "13910764604");
            sportmap.put("client", "andro");

           
      //     String url = "http://123.57.152.60:80/kwhealth/server/evaluationInterface.do";
//        String url = "http://hdhealthtest.taikang.com/hdhealth/app/doctor/diabetes/login.do";
//           String url = "http://localhost:8080/hdhealth/app/doctor/diabetes/login.do";
           String url = "http://139.129.29.11:8088/hdhealth/app/doctor/diabetes/login.do";
        jsonStr = JSONObject.fromObject(getOutMap(sportmap)).toString();
        System.out.println(jsonStr);
            String result =  httpGetUrl(url,jsonStr);
            System.out.println(result);
            result = getInString(result);
            System.out.println(result);
            JSONObject obj = JSONObject.fromObject(result);
            uid=obj.getString("uid");
            return obj.getString("sessionid");
        }
        
        /**5.3 获取手机验证码接口
         * 
          */
         public static String identify()
         {
         	String jsonStr = "";
         	Map sportmap = new HashMap();
             sportmap.put("method", "identify");
             sportmap.put("version", "1.0");
             sportmap.put("mobile", "139107646045");
             
             
            
       //     String url = "http://123.57.152.60:80/kwhealth/server/evaluationInterface.do";
         String url = "http://116.58.221.13:80/hdhealth/app/doctor/diabetes/login.do";
             //String url = "http://192.168.1.230:7001/health/app/doctor/diabetes/login.do";
         jsonStr = JSONObject.fromObject(getOutMap(sportmap)).toString();
         System.out.println(jsonStr);
             String result =  httpGetUrl(url,jsonStr);
             System.out.println(result);
             result = getInString(result);
             System.out.println(result);
             JSONObject obj = JSONObject.fromObject(result);
             
             return obj.getString("sessionid");
         }
         
         /**5.3 忘记密码接口
          * 
           */
          public static String passreset()
          {
          	String jsonStr = "";
          	Map sportmap = new HashMap();
              sportmap.put("method", "passreset");
              sportmap.put("version", "1.0");
              sportmap.put("mobile", "13001129088");
              sportmap.put("password", "123");
              sportmap.put("token", "test");
              sportmap.put("client", "andro");
              
             
        //     String url = "http://123.57.152.60:80/kwhealth/server/evaluationInterface.do";
          String url = "http://localhost:8080/hdhealth/app/doctor/doctor/login.do";
              //String url = "http://192.168.1.230:7001/health/app/doctor/diabetes/login.do";
          jsonStr = JSONObject.fromObject(getOutMap(sportmap)).toString();
          System.out.println(jsonStr);
              String result =  httpGetUrl(url,jsonStr);
              System.out.println(result);
              result = getInString(result);
              System.out.println(result);
              JSONObject obj = JSONObject.fromObject(result);
              
              return obj.getString("sessionid");
          }
          /**5.5 保存基本信息接口
           * 
            */
           public static String saveBaseinfo(String sessionid)
           {
           	String jsonStr = "";
           	Map sportmap = new HashMap();
               sportmap.put("method", "saveBaseinfo");
               sportmap.put("version", "1.0");
               sportmap.put("sessionid", sessionid);
               sportmap.put("uid", "74c7018c-f6b7-4020-b10f-826d726c9781");
               sportmap.put("height", "170");
               sportmap.put("weight", "55.6");
               sportmap.put("usertype", "0");
               sportmap.put("breakfirst", "07:00");
               sportmap.put("lunch", "12:00");
               sportmap.put("dinner", "19:00");
               sportmap.put("sleep", ":00");
               sportmap.put("sugar", "6.0");
               sportmap.put("hemoglobin", "7.0");
               sportmap.put("sugarafter", "8.0");
             
              
               String url = "http://116.58.221.13:80/hdhealth/app/doctor/diabetes/user.do";
               
           jsonStr = JSONObject.fromObject(getOutMap(sportmap)).toString();
           System.out.println(jsonStr);
               String result =  httpGetUrl(url,jsonStr);
               System.out.println(result);
               result = getInString(result);
               System.out.println(result);
               return result;
           }
           /**5.6 获取基本信息接口
            * 
             */
            public static String getBaseinfo(String sessionid)
            {
            	String jsonStr = "";
            	Map sportmap = new HashMap();
                sportmap.put("method", "getBaseinfo");
                sportmap.put("version", "1.0");
                sportmap.put("sessionid", sessionid);
                sportmap.put("uid", uid);
                sportmap.put("date", "2015-11-26");
                
                
               
                //String url = "http://116.58.221.13:80/hdhealth/app/doctor/doctor/user.do";
                String url = "http://hdhealthtest.taikang.com/hdhealth/app/doctor/diabetes/user.do";
                
                jsonStr = JSONObject.fromObject(getOutMap(sportmap)).toString();
            System.out.println(jsonStr);
                String result =  httpGetUrl(url,jsonStr);
                System.out.println(result);
                result = getInString(result);
                System.out.println(result);
                return result;
            }
            /**5.6 获取基本信息接口
             * 
              */
             public static String getSdf(String sessionid)
             {
             	String jsonStr = "";
             	Map sportmap = new HashMap();
                 sportmap.put("method", "getJqys");
                 sportmap.put("version", "1.0");
                 sportmap.put("sessionid", sessionid);
                 sportmap.put("uid", "");
                 sportmap.put("index", "21");
                 sportmap.put("tz", "1");
                 
                 
                
                 //String url = "http://116.58.221.13:80/hdhealth/app/doctor/doctor/user.do";
//                 String url = "http://hdhealthtest.taikang.com/hdhealth/app/doctor/doctor/evaOfTraChiMedicine.do";
                 String url = "http://192.168.2.209:8080/hdhealth/app/doctor/doctor/evaOfTraChiMedicine.do";
//                 String url = "http://192.168.1.133:8080/hdhealth/app/doctor/doctor/evaOfTraChiMedicine.do";
                 jsonStr = JSONObject.fromObject(getOutMap(sportmap)).toString();
             System.out.println(jsonStr);
                 String result =  httpGetUrl(url,jsonStr);
                 System.out.println(result);
                 result = getInString(result);
                 System.out.println(result);
                 return result;
             }
            
            /**5.7 保存血糖测量计划
             * 
              */
             public static String saveSugarPlan(String sessionid)
             {
             	String jsonStr = "";
             	Map sportmap = new HashMap();
                 sportmap.put("method", "saveSugarPlan");
                 sportmap.put("version", "1.0");
                 sportmap.put("sessionid", sessionid);
                 sportmap.put("uid", "74c7018c-f6b7-4020-b10f-826d726c9781");
                 sportmap.put("date", "2015-09-17");
                 sportmap.put("timespan", "0");
                 sportmap.put("value", "0");
                
               
                
                 String url = "http://116.58.221.13:80/hdhealth/app/doctor/diabetes/sugar.do";
                 
             jsonStr = JSONObject.fromObject(getOutMap(sportmap)).toString();
             System.out.println(jsonStr);
                 String result =  httpGetUrl(url,jsonStr);
                 System.out.println(result);
                 result = getInString(result);
                 System.out.println(result);
                 return result;
             }
             
             /**5.8 获取血糖测量计划
              * 
               */
              public static String getSugarPlan(String sessionid)
              {
              	String jsonStr = "";
              	Map sportmap = new HashMap();
                  sportmap.put("method", "getSugarPlan");
                  sportmap.put("version", "1.0");
                  sportmap.put("sessionid", sessionid);
                  sportmap.put("uid", uid);
                  sportmap.put("startDate", "2015-10-08");
                  sportmap.put("endDate", "2016-10-28");
                  
                
                 
                  String url = "http://localhost:8080/hdhealth/app/doctor/diabetes/sugar.do";
                  
              jsonStr = JSONObject.fromObject(getOutMap(sportmap)).toString();
              System.out.println(jsonStr);
                  String result =  httpGetUrl(url,jsonStr);
                  System.out.println(result);
                  result = getInString(result);
                  System.out.println(result);
                  return result;
              }
              
              /**5.9 获取提醒消息列表
               * 
                */
               public static String getWarnList(String sessionid)
               {
               	String jsonStr = "";
               	Map sportmap = new HashMap();
                   sportmap.put("method", "getWarnList");
                   sportmap.put("version", "1.0");
                   sportmap.put("sessionid", sessionid);
                   sportmap.put("uid", uid);
                   sportmap.put("date", "2015-09-08");
                   
                 
                  
                   String url = "http://116.58.221.13:80/hdhealth/app/doctor/diabetes/warn.do";
                   
               jsonStr = JSONObject.fromObject(getOutMap(sportmap)).toString();
               System.out.println(jsonStr);
                   String result =  httpGetUrl(url,jsonStr);
                   System.out.println(result);
                   result = getInString(result);
                   System.out.println(result);
                   return result;
               }
               public static String getReportList(String sessionid)
               {
               	String jsonStr = "";
               	Map sportmap = new HashMap();
                   sportmap.put("method", "getReportList");
                   sportmap.put("version", "1.0");
                   sportmap.put("sessionid", sessionid);
                   sportmap.put("uid", uid);
                   sportmap.put("pages", "1");
                   sportmap.put("date", "2016-01-02");
                   
                 
                  
//                   String url = "http://139.129.29.11:8088/hdhealth/app/doctor/diabetes/report.do";
                   String url = "http://localhost:8080/hdhealth/app/doctor/diabetes/report.do";
                   
               jsonStr = JSONObject.fromObject(getOutMap(sportmap)).toString();
               System.out.println(jsonStr);
                   String result =  httpGetUrl(url,jsonStr);
                   System.out.println(result);
                   result = getInString(result);
                   System.out.println(result);
                   return result;
               }
               
               public static String getMedicinePlan(String sessionid)
               {
               	String jsonStr = "";
               	Map sportmap = new HashMap();
                   sportmap.put("method", "getMedicinePlan");
                   sportmap.put("version", "1.0");
                   sportmap.put("sessionid", sessionid);
                   sportmap.put("uid", uid);
                 //  sportmap.put("pages", "1");
                   sportmap.put("date", "2016-01-13");
                   
                 
                  
                   String url = "http://139.129.29.11:8088/hdhealth/app/doctor/diabetes/medicine.do";
//                   String url = "http://localhost:8080/hdhealth/app/doctor/diabetes/medicine.do";
                   
               jsonStr = JSONObject.fromObject(getOutMap(sportmap)).toString();
               System.out.println(jsonStr);
                   String result =  httpGetUrl(url,jsonStr);
                   System.out.println(result);
                   result = getInString(result);
                   System.out.println(result);
                   return result;
               }
               
               /**5.10 提醒消息状态
                * 
                 */
                public static String setWarnState(String sessionid)
                {
                	String jsonStr = "";
                	Map sportmap = new HashMap();
                    sportmap.put("method", "setWarnState");
                    sportmap.put("version", "1.0");
                    sportmap.put("sessionid", sessionid);
                    sportmap.put("uid", uid);
                    sportmap.put("warnId", "111111");
                    sportmap.put("isRead", "1");
                    
                  
                   
                    String url = "http://116.58.221.13:80/hdhealth/app/doctor/diabetes/warn.do";
                    
                jsonStr = JSONObject.fromObject(getOutMap(sportmap)).toString();
                System.out.println(jsonStr);
                    String result =  httpGetUrl(url,jsonStr);
                    System.out.println(result);
                    result = getInString(result);
                    System.out.println(result);
                    return result;
                }
                
                /**5.11 获取提醒消息数量
                 * 
                  */
                 public static String getWarnCount(String sessionid)
                 {
                 	String jsonStr = "";
                 	Map sportmap = new HashMap();
                     sportmap.put("method", "getWarnCount");
                     sportmap.put("version", "1.0");
                     sportmap.put("sessionid", sessionid);
                     sportmap.put("uid", uid);
                     sportmap.put("date", "2015-09-16");
                     
                   
                    
                     String url = "http://116.58.221.13:80/hdhealth/app/doctor/diabetes/warn.do";
                     
                 jsonStr = JSONObject.fromObject(getOutMap(sportmap)).toString();
                 System.out.println(jsonStr);
                     String result =  httpGetUrl(url,jsonStr);
                     System.out.println(result);
                     result = getInString(result);
                     System.out.println(result);
                     return result;
                 }
                 
                 /**5.11 获取糖化血红蛋白
                  * 
                   */
                  public static String getHemoglobinList(String sessionid)
                  {
                  	String jsonStr = "";
                  	Map sportmap = new HashMap();
                      sportmap.put("method", "getHemoglobinList");
                      sportmap.put("version", "1.0");
                      sportmap.put("sessionid", sessionid);
                      sportmap.put("uid", uid);
                      sportmap.put("startdate", "2015-09-06");
                      sportmap.put("enddate", "2015-09-16");
                      
                    
                     
                      String url = "http://116.58.221.13:80/hdhealth/app/doctor/diabetes/hemoglobin.do";
                      
                  jsonStr = JSONObject.fromObject(getOutMap(sportmap)).toString();
                  System.out.println(jsonStr);
                      String result =  httpGetUrl(url,jsonStr);
                      System.out.println(result);
                      result = getInString(result);
                      System.out.println(result);
                      return result;
                  }
                  
                  /**5.29 保存运动测量值
                   * 
                    */
                   public static String saveSport(String sessionid)
                   {
                   	String jsonStr = "";
                   	Map sportmap = new HashMap();
                       sportmap.put("method", "saveSport");
                       sportmap.put("version", "1.0");
                       sportmap.put("sessionid", sessionid);
                       sportmap.put("uid", "74c7018c-f6b7-4020-b10f-826d726c9781");
                       
                       Map soprtlist = new HashMap();
                       soprtlist.put("startdate", "2015-09-17 08:08:43");
                       soprtlist.put("enddate", "2015-09-17 08:08:53");
                       soprtlist.put("stepnum", "0");
                       soprtlist.put("value", "20");
                       soprtlist.put("sporttypecd", "1");
                       
                       Map soprtlist2 = new HashMap();
                       soprtlist2.put("startdate", "2015-09-17 08:08:43");
                       soprtlist2.put("enddate", "2015-09-17 08:08:53");
                       soprtlist2.put("stepnum", "0");
                       soprtlist2.put("value", "20");
                       soprtlist2.put("sporttypecd", "1");
                       
                       Map[] splist = new HashMap[2];
                       splist[0] = soprtlist;
                       splist[1] = soprtlist2;
                       sportmap.put("sportlist", splist);
                      
                     
                      
                       String url = "http://116.58.221.13:80/hdhealth/app/doctor/diabetes/sport.do";
                       System.out.println(JSONObject.fromObject(sportmap).toString());
                   jsonStr = JSONObject.fromObject(getOutMap(sportmap)).toString();
                   System.out.println(jsonStr);
                       String result =  httpGetUrl(url,jsonStr);
                       System.out.println(result);
                       result = getInString(result);
                       System.out.println(result);
                       return result;
                   }
        
        /**获取药品列表
         * 
          */
         public static String getMedicine(String sessionid)
         {
         	String jsonStr = "";
         	Map sportmap = new HashMap();
             sportmap.put("method", "getHistory");
             sportmap.put("version", "1.0");
             sportmap.put("Sessionid", sessionid);
             sportmap.put("uid", "26c8a678-4681-4584-a555-1a13c4215f8e");
           
            
       //     String url = "http://123.57.152.60:80/kwhealth/server/evaluationInterface.do";
//         String url = "http://hdhealthtest.taikang.com:80/hdhealth/app/doctor/diabetes/medicine.do";
         String url = "http://localhost:8080/hdhealth/app/doctor/diabetes/medicine.do";
         jsonStr = JSONObject.fromObject(getOutMap(sportmap)).toString();
           
             String result =  httpGetUrl(url,jsonStr);
            // JSONObject obj = JSONObject.fromObject(result);
             System.out.println(result);
             return result;
         }
         
      // 辅助函数: 使用 GET 方式发起一个 HTTP 请求, 并返回结果
         private static String httpGet(String url)
         {
         
              
             String response=new String();
             CloseableHttpClient client = HttpClients.createDefault();
            
             //使用POST方法
             HttpGet method = new HttpGet(url);  
            
             
             
             try{
            	 CloseableHttpResponse httpResponse =   client.execute(method);
            	 HttpEntity entity = httpResponse.getEntity();
                 if (null != entity){
                	 
	                 if(httpResponse.getStatusLine().getStatusCode()==200)
	                 {
	                     response = EntityUtils.toString(entity);
	                 }
                 }
             }catch (Exception ex)
             {
                ex.printStackTrace();
             }

             
            
             //释放连接
             method.releaseConnection();
             // 转换编码
             //return Utility.utf8Togb2312(response);
             return response;
         }
        
     // 辅助函数: 使用 POST 方式发起一个 HTTP 请求, 并返回结果
        private static String httpGetUrl(String url,String jsonStr)
        {
        	
        	 NameValuePair value0 =  new NameValuePair("jsonStr", jsonStr);
             NameValuePair value1 =  new NameValuePair("checkStr", stringMD5(jsonStr));

             NameValuePair[] orderInfo = {value0,value1};
             
            String response=new String();
            HttpClient client = new HttpClient();
           
            //使用POST方法
            PostMethod method = new UTF8PostMethod(url);
            
            method.setRequestBody(orderInfo);
         System.out.println(method.getRequestCharSet());
         System.out.println(method.getResponseCharSet());
            
            
            try{
                client.executeMethod(method);
                
                if(method.getStatusCode()==200)
                {
                    response = method.getResponseBodyAsString();
                }
            }catch (Exception ex)
            {
               ex.printStackTrace();
            }

            
           
            //释放连接
            method.releaseConnection();
            // 转换编码
            //return Utility.utf8Togb2312(response);
            return response;
        }
        //处理编码问题
        private static class UTF8PostMethod extends PostMethod {
           public UTF8PostMethod(String url) {
           super(url);
           }

           @Override
           public String getRequestCharSet() {
           //return super.getRequestCharSet();
           return "UTF-8";
           }
           @Override
           public String getResponseCharSet() {
          // return super.getResponseCharSet();
           return "UTF-8";
           }
        }
        /**
         * 获取加密后的字符串
         * @param input
         * @return
         */
        public static String stringMD5(String pw) {
         try { 

               // 拿到一个MD5转换器（如果想要SHA1参数换成”SHA1”） 
               MessageDigest messageDigest =MessageDigest.getInstance("MD5"); 
               // 输入的字符串转换成字节数组 
               byte[] inputByteArray = pw.getBytes(); 
               // inputByteArray是输入字符串转换得到的字节数组 
               messageDigest.update(inputByteArray); 
               // 转换并返回结果，也是字节数组，包含16个元素 
               byte[] resultByteArray = messageDigest.digest(); 
               // 字符数组转换成字符串返回 
               return byteArrayToHex(resultByteArray); 
            } catch (NoSuchAlgorithmException e) { 
               return null; 
            } 
        }
        

           public static String byteArrayToHex(byte[] byteArray) { 

               // 首先初始化一个字符数组，用来存放每个16进制字符 
               char[] hexDigits = {'9','8','7','6','5','4','3','2','1','0', 'A','B','C','D','E','F' }; 
               // new一个字符数组，这个就是用来组成结果字符串的（解释一下：一个byte是八位二进制，也就是2位十六进制字符（2的8次方等于16的2次方）） 
               char[] resultCharArray =new char[byteArray.length * 2]; 
               // 遍历字节数组，通过位运算（位运算效率高），转换成字符放到字符数组中去 
               int index = 0;
               for (byte b : byteArray) { 
                  resultCharArray[index++] = hexDigits[b>>> 4 & 0xf]; 
                  resultCharArray[index++] = hexDigits[b& 0xf]; 
               }
               // 字符数组组合成字符串返回 
               return new String(resultCharArray); 
           }
           
           private static String getInString(String taikangtext) {
       		String result = "";
       		JSONObject robj = new JSONObject();
       		JSONObject sobj = JSONObject.fromObject(taikangtext);
       		if(sobj.getString("resultcode").equals("0")){
       		robj.put("resultcode", sobj.getString("resultcode"));
       		}else if(sobj.getString("resultcode").equals("100020")){
       			robj.put("resultcode", "3");
       		}else if(sobj.getString("resultcode").equals("100016")){
       			robj.put("resultcode", "2");
       		}else if(sobj.getString("resultcode").equals("100021")){
       			robj.put("resultcode", "4");
       		}else{
       			robj.put("resultcode", "1");
       		}
       		robj.put("resultmsg", sobj.getString("resultmsg"));
       		if (sobj.getString("sessionid") != null) {
       			robj.put("sessionid", sobj.getString("sessionid"));
       		}
       		if (sobj.getString("uid") != null) {
       			robj.put("uid", sobj.getString("uid"));
       		}
       		if(sobj.has("resultlist")){
       		JSONArray array = sobj.getJSONArray("resultlist");
       		if(taikangtext.indexOf("breakfastPre")>0){
       			if(!array.get(0).equals(null)){
       			robj.put("planList", array);
       			}else{
       				robj.put("planList", array);
       			}
       		}else if(taikangtext.indexOf("warnType")>0){
       			if(!array.get(0).equals(null)){
           			robj.put("warnList", array);
           			}else{
           				robj.put("warnList", array);
           			}
           		}else{
       		if (array != null && array.size() > 0) {
       			for (int i = 0; i < array.size(); i++) {
       				try {
       					
       					if (!array.get(i).equals(null)) {
       						
       						
       						JSONObject temp = array.getJSONObject(i);
       						Iterator entrySet = temp.keys();
       						while (entrySet.hasNext()) {
       							String tempKey = (String) entrySet.next();
       							try {
       								robj.put(tempKey, temp.get(tempKey));
       							} catch (Exception ex) {
       								ex.printStackTrace();
       							}
       						}
       						
       					}
       				} catch (Exception e) {
       					e.printStackTrace();
       				}

       			}
       		}
       		}
       		}
       		result = robj.toString();

       		return result;

       	}
           
           private static Map getOutMap(Map inmap) {

       		Map result = new HashMap();
       		Object[] objs = new Object[1];
       		Map requestlist = new HashMap();

       		HashMap basePara = new HashMap();
       		basePara.put("requestsource", "");
       		basePara.put("requestid", "");
       		basePara.put("uid", "");
       		basePara.put("sessionid", "");
       		basePara.put("method", "");
       		basePara.put("version", "");

       		if (inmap != null) {
       			result.put("requestsource", "diabetes");
       			result.put("requestid", "");
       			result.put("uid", "");
       			result.put("sessionid", "");
       			result.put("method", "");
       			result.put("version", "");

       			Set<Entry<String, Object>> entrySet = inmap.entrySet();
       			
       			for (Entry<String, Object> entry : entrySet) {
       				if (basePara.containsKey(entry.getKey())) {
       					result.put(entry.getKey(), entry.getValue());
       				} else if(entry.getKey().equals("sportlist") || entry.getKey().equals("mealdetail")){
       					objs = (Map[])entry.getValue();
       					
       				}else {
       					requestlist.put(entry.getKey(), entry.getValue());
       					objs[0] = requestlist;
       				}
       			}
       			
       			result.put("requestlist", objs);
       		}

       		return result;
       	}
}
